package com.application.mylibrary.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.application.mylibrary.R;
import com.application.mylibrary.utils.EventBusUtils;

/**
 * Created by lixuce on 2019/8/6.
 */
public abstract class BaseFragment extends Fragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        beforeInit(arguments);
        if (registerEventBus()) {
            EventBusUtils.register(this);
        }
    }

    public boolean registerEventBus() {
        return false;
    }

    protected abstract void beforeInit(Bundle arguments);

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (getLayoutId()!=0) {
            View view = inflater.inflate(getLayoutId(), container, false);
            return view;
        }
       return null;
    }

    public Context mCOntext;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mCOntext = context;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    public abstract int getLayoutId();

    public abstract void initView(View view);

    //带有动画的跳转
    public void startActivityAnim(Intent intent) {
        startActivity(intent);
        if (!isFinish()) {
            Context context = getContext();
            if (context instanceof Activity) {
                ((Activity) context)
                        .overridePendingTransition(0,0);
            }
        }
    }

    public boolean isFinish() {
        Context context = getContext();
        if (context instanceof Activity) {
            if (((Activity) context).isFinishing() || ((Activity) context).isDestroyed()) {
                return true;
            }
        }
        return false;
    }


    //显示load弹窗
    public void showloadDialog() {

    }

    //loading弹窗消失
    public void hideLoadDialog() {

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (registerEventBus()) {
            EventBusUtils.unregister(this);
        }
    }
}
